﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace IkeCode.WP7.Toolkit.Helpers
{
    /// <summary>
    /// Class to help on Create, Delete and Update Live Tiles
    /// </summary>
    public static class IcTileHelper
    {
        /// <summary>
        /// Pin a Tile to the Home Screen, if already pinned will update
        /// </summary>
        /// <param name="pageUri">Page Uri for navigate to on tap tile</param>
        /// <param name="title">Tile title</param>
        /// <param name="secondaryTitle">Tile secondary title</param>
        /// <param name="secondaryContent">Title secondary content</param>
        /// <param name="count">News count</param>
        /// <param name="backgroundUri">Background image uri</param>
        /// <param name="secondaryBackgroundUri">Background image  uri secondary</param>
        public static void PinTile(Uri pageUri, string title, string secondaryTitle, string secondaryContent, int count = 0, Uri backgroundUri = null, Uri secondaryBackgroundUri = null)
        {
            StandardTileData standardTileData = new StandardTileData();
            standardTileData.BackgroundImage = backgroundUri;
            standardTileData.Title = title;
            standardTileData.Count = count;
            standardTileData.BackTitle = secondaryTitle;
            standardTileData.BackContent = secondaryContent;
            standardTileData.BackBackgroundImage = secondaryBackgroundUri;

            ShellTile tileToPin = ShellTile.ActiveTiles.FindTile(pageUri);
            if (tileToPin == null)
            {
                ShellTile.Create(pageUri, standardTileData);
            }
            else
            {
                tileToPin.Update(standardTileData);
            }
        }

        /// <summary>
        /// Unpin Tile from Home Screen
        /// </summary>
        /// <param name="pageUri">Tile target page Uri</param>
        public static void UnpinTile(Uri pageUri)
        {
            ShellTile tileToPin = ShellTile.ActiveTiles.FindTile(pageUri);
            if (tileToPin != null)
            {
                tileToPin.Delete();
            }
        }
    }
}
